 //
//  ProductViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "ProductViewController.h"
#import "RESideMenu.h"
#import "MainDataHelper.h"
#import "ProductModel.h"
#import "ProductTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ProductDetailViewController.h"
#import "MJRefresh.h"
@interface ProductViewController ()<UITableViewDataSource,UITableViewDelegate>

- (IBAction)Back:(UIBarButtonItem *)sender;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger *num;

@end

@implementation ProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self loadTableView];
    [self steputRefresh];
    
    _num = 10;
//    [_dataArray count]?[[[[_dataArray objectAtIndex:0] objectForKey:@"subs"] objectAtIndex:0] objectForKey:@"section"]:@"";
//    
//    [_dataArray count]?[[[_dataArray objectAtIndex:0] objectForKey:@"subs"] count]:0;
    
//    
}

#pragma mark --- tableView部分
- (void)loadTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    UINib *Nib = [UINib nibWithNibName:@"ProductTableViewCell" bundle:nil];
    [_tableView registerNib:Nib forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tableView];
    
    self.navigationController.navigationBar.translucent = NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;

}


- (ProductTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    ProductModel *model = self.dataArray[indexPath.row];
    cell.productLabel.text = model.title;
    [cell.prductImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
    // 取消cell的高亮状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}


// 懒加载
- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark --- 数据请求部分
- (void)loadData{
    
//    [self.dataArray removeAllObjects];
    NSString *string = @"http://api2.pianke.me/pub/shop";
    NSString *bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=%ld&start=0&version=3.0.6",(long)_num] ;
    [MainDataHelper postDataWithUrlString:string andBodyString:bodyString compare:^(id object) {
//        _dataArray = [NSMutableArray array];
        NSDictionary *dic = object;
        
        NSArray *array = dic[@"data"][@"list"];
        
        for (NSDictionary *dic1 in array) {
            ProductModel *model = [[ProductModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        [_tableView reloadData];
        
    }];
    
    // 结束下拉刷新
    [self.tableView.header endRefreshing];
    // 结束上拉加载
    [self.tableView.footer endRefreshing];
    
}

// 上拉加载,下拉刷新方法
- (void)steputRefresh{
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadData];
        
    }];
    
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        [self loadData];
        self.num += 10;
        
    }];
    
}


#pragma mark --- cell点击方法部分

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductDetailViewController *proVC = [[ProductDetailViewController alloc] init];
    [self.navigationController pushViewController:proVC animated:YES];
    if (_dataArray.count == 0) {
        return ;
    }
    ProductModel *model = _dataArray[indexPath.row];
    proVC.conStr = model.contentid;
   
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)Back:(UIBarButtonItem *)sender {
    
    [self.sideMenuViewController presentLeftMenuViewController];
    
}
@end
